package com.molichuxing.service.impl;

import com.molichuxing.service.CarFenceExceptionJobService;
import com.molichuxing.services.business.dto.response.StockCarBizDto;
import com.molichuxing.services.business.service.StockCarBizService;
import com.molichuxing.services.infrastructure.dto.request.create.FenceExceptionCreateDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.IovFenceDto;
import com.molichuxing.services.infrastructure.service.*;
import com.molichuxing.services.property.FenceStoreStockTypeEnum;
import com.molichuxing.services.property.StockCarStockTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆围栏异常任务service
 *
 * @author xuelin yi
 * @date 2019年12月24日
 */
@Service("carFenceExceptionJobService")
public class CarFenceExceptionJobServiceImpl implements CarFenceExceptionJobService {
    private static final Logger logger = LoggerFactory.getLogger(CarFenceExceptionJobServiceImpl.class);

    @Resource
    private StockCarBizService stockCarBizService;

    @Resource
    private IovFenceService iovFenceService;

    @Resource
    private CarService carService;

    @Resource
    private CarFenceExceptionService carFenceExceptionService;

    @Resource
    private FenceStoreStockService fenceStoreStockService;

    @Resource
    private StockCarService stockCarService;

    @Override
    public Boolean getFenceExceptionCar() {
        //获取在中转库围栏或在门店围栏且未运营车辆信息
        List<StockCarBizDto> stockCarBizDtoList = stockCarBizService.getStockListByStatus();
        logger.info("未获取在中转库围栏或在门店围栏且未运营车辆信息"+stockCarBizDtoList.size());
        if((null == stockCarBizDtoList)||(stockCarBizDtoList.size() == 0)) {
            logger.info("未获取在中转库围栏或在门店围栏且未运营车辆信息");
            return true;
        }
        List<Integer> carIds = stockCarBizDtoList.stream().map(StockCarBizDto::getCarId).collect(Collectors.toList());
        //删除用户提走得车等其他数据
        logger.info("删除用户提走得车等其他数据开始");
        carFenceExceptionService.deleteFenceData(getNeedAddCarIdList(carIds, carFenceExceptionService.getAllCarId()));
        logger.info("删除用户提走得车等其他数据结束");
        if((null == carIds)||(carIds.size() == 0)) {
            logger.info("转换车辆id为空");
            return true;
        }
        Map<Integer, CarDto> carMap = carService.getCarByIds(carIds);
        if((null == carMap)||(carMap.size() == 0)) {
            logger.info("根据车辆id查询车辆信息为空");
            return true;
        }
        List<String> vinList = carMap.values().stream().map(CarDto::getVin).collect(Collectors.toList());
        if((null == vinList)||(vinList.size() == 0)) {
            logger.info("根据车辆id获取车辆的vin为空");
            return true;
        }
        Map<String,List<IovFenceDto>> iovFenceMap = iovFenceService.getFenceByVins(vinList);
        for(StockCarBizDto stockCarBizDto : stockCarBizDtoList) {
            if(null == stockCarBizDto.getFenceId()) {
                carFenceExceptionService.createCarFenceException(encapData(stockCarBizDto, null, "车辆在OP未绑定围栏"));
                continue;
            }
            CarDto opCarDto = carMap.get(stockCarBizDto.getCarId());
            if(null != opCarDto) {
                List<IovFenceDto> iovFenceDtoList = iovFenceMap.get(opCarDto.getVin());
                //车联网未绑定围栏
                if((null == iovFenceDtoList)||(iovFenceDtoList.size() == 0)) {
                    carFenceExceptionService.createCarFenceException(encapData(stockCarBizDto, null, "车辆未绑定仓储下的围栏"));
                    continue;
                }
                if(iovFenceDtoList.get(0).getId().equals(stockCarBizDto.getFenceId())) {
                    //车联网和op绑定的围栏一致
                    carFenceExceptionService.deleteFenceData(Arrays.asList(stockCarBizDto.getCarId()));
                    continue;
                }
                List<Integer> opFenceIds = null;
                if (stockCarBizDto.getStockType() == StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue()) {
                    opFenceIds = fenceStoreStockService.getFenceByStoreId(stockCarBizDto.getStockId(), FenceStoreStockTypeEnum.ENTREPOT_STORAGE);

                }else if(stockCarBizDto.getStockType() == StockCarStockTypeEnum.STORE.getValue()) {
                    opFenceIds = fenceStoreStockService.getFenceByStoreId(stockCarBizDto.getStoreId(), FenceStoreStockTypeEnum.STORE);
                }
                if((null == opFenceIds)||(opFenceIds.size() == 0)) {
                    carFenceExceptionService.createCarFenceException(encapData(stockCarBizDto, iovFenceDtoList.get(0).getId(), "车辆绑定仓储与车辆绑定围栏不符"));
                }
                // op车辆所在的仓储下的围栏不包括车联网的围栏
                if(!opFenceIds.contains(iovFenceDtoList.get(0).getId())) {
                    carFenceExceptionService.createCarFenceException(encapData(stockCarBizDto, iovFenceDtoList.get(0).getId(), "车辆绑定仓储与车辆绑定围栏不符"));
                }else{
                    //讲op车辆所在的围栏修改为跟车联网一致
                    // 修改op  所在的围栏StockCarService
                    stockCarService.modifyFenceId(stockCarBizDto.getCarId(), iovFenceDtoList.get(0).getId());
                    carFenceExceptionService.deleteFenceData(Arrays.asList(stockCarBizDto.getCarId()));
                }
            }
        }
        return true;
    }


    private FenceExceptionCreateDto encapData(StockCarBizDto stockCarBizDto, Integer iovFenceId, String explain) {
        FenceExceptionCreateDto fenceExceptionCreateDto = new FenceExceptionCreateDto();
        fenceExceptionCreateDto.setCarId(stockCarBizDto.getCarId());
        fenceExceptionCreateDto.setType(stockCarBizDto.getStockType());
        if(stockCarBizDto.getStockType() == StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue()) {
            fenceExceptionCreateDto.setStockId(stockCarBizDto.getStockId());
        }
        if(stockCarBizDto.getStockType() == StockCarStockTypeEnum.STORE.getValue()) {
            fenceExceptionCreateDto.setStockId(stockCarBizDto.getStoreId());
        }
        if(null != iovFenceId) {
            fenceExceptionCreateDto.setFenceId(iovFenceId);
        }
        fenceExceptionCreateDto.setExplain(explain);
        return fenceExceptionCreateDto;
    }

    private static List<Integer> getNeedAddCarIdList(List<Integer> newCarIds, List<Integer> oldCarIds) {
        if(null == oldCarIds || oldCarIds.size() == 0) {
            return null;
        }
        if(null == newCarIds || newCarIds.size() == 0) {
            return oldCarIds;
        }
        Map<Integer, Integer> dataMap = new HashMap<>();
        for (Integer id : newCarIds) {
            dataMap.put(id, id);
        }

        List<Integer> newList = new ArrayList<>();
        for (Integer id : oldCarIds) {
            if (!dataMap.containsKey(id)) {
                newList.add(id);
            }
        }
        return newList;
    }

}
